package com.unhz.taigu.controller.login;

import com.unhz.generator.pojo.LoginInfo;
import com.unhz.generator.pojo.SysUser;
import com.unhz.generator.utils.EmptyUtil;
import com.unhz.taigu.controller.shiro.ActiveUser;
import com.unhz.taigu.service.LoginInfoService;
import com.unhz.taigu.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

/**
 * Created by cxg on 2017/11/14 0014.
 *
 * @Author 曹显贵
 * @Date 2017/11/14 0014 12:47
 */
@Controller
public class SysLoginUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private LoginInfoService loginInfoService;

    @RequestMapping("/login.do")
    public String userLogin(HttpServletRequest request) throws Exception {
        String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
        if(exceptionClassName!=null){
            if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
                //最终会抛给异常处理器
                request.getSession().setAttribute("error","<span color='red'>账号不存在</span>");
                return "login.jsp";
            } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
                request.getSession().setAttribute("error","<span color='red'>密码或者用户名错误,请重新登录！</span>");
                return "login.jsp";
            }else {
                throw new Exception();//最终在异常处理器生成未知错误
            }
        }
        if(!EmptyUtil.isEmpty((ActiveUser)SecurityUtils.getSubject().getPrincipal())){
            return "main.jsp";
        }else{
            return "login.jsp";
        }
    }

    @RequestMapping(value="/firstIndex.do")
    public String firstClass(Model model, HttpServletRequest request){
        Subject subject = SecurityUtils.getSubject();
        ActiveUser activeUser = (ActiveUser)subject.getPrincipal();
        SysUser sysUser = sysUserService.selectByUserId(activeUser.getId());
        //添加用户登录信息
       Integer userId = sysUser.getId();
        String ip = null;
        try {
           InetAddress myip= InetAddress.getLocalHost();
           ip = myip.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUserId(userId);
        loginInfo.setIpAddress(ip);
        loginInfo.setLoginTime(new Date());
        loginInfoService.insertLoginInfo(loginInfo);
        //添加用户登录信息
        request.getSession().setAttribute("sysUserManage", sysUser);
        return "main.jsp";
    }
}
